package com.shimao.celnet.payment.controller.apinotcheck;

import com.shimao.celnet.payment.config.ConstantConfig;
import com.shimao.celnet.payment.form.IAppInterfaceForm;
import com.shimao.celnet.payment.model.PInvoiceBlue;
import com.shimao.celnet.payment.model.PInvoiceRed;
import com.shimao.celnet.payment.util.GetJFSystemToken;
import com.shimao.celnet.payment.util.HttpSendPostUtil;
import com.shimao.celnet.payment.util.SignCheckUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.TreeMap;

/**
 * 打印结果通知
 * 2018/01/03
 * create by stan.ruan
 * 世茂支付平台 → 外部系统；
 * 【数据】：打印结果信息；
 */
@Service
public class CallOut_invPrintRetInfoOutController {
    @Autowired
    private HttpSendPostUtil httpSendPostUtil;

    @Autowired
    private ConstantConfig constantConfig;

    @Autowired
    private GetJFSystemToken getJFSystemToken;
    /**
     * 打印结果通知
     * @param pInvoiceBlue  打印蓝票
     * @param pInvoiceRed  打印红票
     */
    public void invOpenRetInfoOutController(IAppInterfaceForm iAppInterface, PInvoiceBlue pInvoiceBlue, PInvoiceRed pInvoiceRed) throws Exception {
        String blueRedType = "";//开票类型
        if(pInvoiceBlue != null){
            blueRedType = "blue";
            //指定通知接口，1005不推送
            if("20180117".equals(pInvoiceBlue.getAppKey())&&"1005".equals(iAppInterface.getInterfaceId())) {
                return;
            }
            //指定通知接口，1008打印异步通知接口
            if("20180117".equals(pInvoiceBlue.getAppKey())&&"1008".equals(iAppInterface.getInterfaceId())){
                /**
                 * 物业计费的接口验证规则
                 * 只接受来自物业的费用
                 */
                String token = getJFSystemToken.getJFSystemNewToken(pInvoiceBlue.getAppKey(),iAppInterface.getInterfaceId());
                //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                System.out.println(getRetJson(blueRedType,pInvoiceBlue,pInvoiceRed)+"    "+token);
                String ret = httpSendPostUtil.sendPost(iAppInterface.getRequestUrl(), getRetJson(blueRedType,pInvoiceBlue,pInvoiceRed),token,pInvoiceBlue.getAppKey(),iAppInterface.getInterfaceId());
                System.out.println(ret);
                return;
            }else{
                String ret = httpSendPostUtil.sendPost(iAppInterface.getRequestUrl(), getRetJson(blueRedType,pInvoiceBlue,pInvoiceRed),pInvoiceBlue.getAppKey(),iAppInterface.getInterfaceId());
            }
        }
        if(pInvoiceRed != null) {
            blueRedType = "red";
            //指定通知接口，1005不推送
            if("20180117".equals(pInvoiceRed.getAppKey())&&"1005".equals(iAppInterface.getInterfaceId())) {
                return;
            }
            if ("20180117".equals(pInvoiceRed.getAppKey())&&"1008".equals(iAppInterface.getInterfaceId())){
                /**
                 * 物业计费的接口验证规则
                 * 只接受来自物业的费用
                 */
                String token = getJFSystemToken.getJFSystemNewToken(pInvoiceBlue.getAppKey(),iAppInterface.getInterfaceId());
                //给物业推送消息 如果业务类型里面包含pty则必定是物业费：pty-101支付业务类型:pty-101：物业缴费；pty-102：物业预缴费；
                String ret = httpSendPostUtil.sendPost(iAppInterface.getRequestUrl(), getRetJson(blueRedType, pInvoiceBlue, pInvoiceRed), token,pInvoiceRed.getAppKey(),iAppInterface.getInterfaceId());
                return;
            }else{
                String ret = httpSendPostUtil.sendPost(iAppInterface.getRequestUrl(), getRetJson(blueRedType, pInvoiceBlue, pInvoiceRed),pInvoiceRed.getAppKey(),iAppInterface.getInterfaceId());
            }
        }
    }

    /**
     * 拼接打印发票返回信息的json
     *
     * @return
     */
    public JSONObject getRetJson(String blueRedType, PInvoiceBlue pInvoiceBlue, PInvoiceRed pInvoiceRed) throws Exception {
        String  invGuid = "";//guid
        String printStatus = "";
        String printDate = "";
        JSONObject jsonObject = new JSONObject();
        if("red".equals(blueRedType)){
            invGuid = pInvoiceRed.getGuid();
            printStatus = pInvoiceRed.getInvoicePrintFlg();//0打印失败，1打印成功
            printDate = pInvoiceRed.getInvoicePrintTime();//打印日期
        }else if("blue".equals(blueRedType)) {
            invGuid =pInvoiceBlue.getGuid();
            printStatus = pInvoiceBlue.getInvoicePrintFlg();//0打印失败，1打印成功
            printDate = pInvoiceBlue.getInvoicePrintTime();//打印日期
        }
        jsonObject.put("receiptGuid", invGuid);//需要开发票的支付系统的发票guid：发票种类=blue：蓝票；蓝票的支付平台数据guid；发票种类=red：红票：红票的支付平台数据guid；
        jsonObject.put("blueRedType", blueRedType);//发票种类：blue：蓝票；red：红票
        jsonObject.put("printStatus", printStatus);//发票打印状态
        jsonObject.put("printDate", printDate);//发票打印日期
        return jsonObject;
    }

    /**
     * json转换成map
     * */
    private TreeMap<String, String> toMap(JSONObject json){
        TreeMap<String, String> map = new TreeMap<String, String>();
        Iterator<String> keys = json.keys();
        while (keys.hasNext()){
            String key = keys.next();
            String value = json.get(key).toString();
            map.put(key, value);
        }
        return map;
    }
}
